package com.heba.popupwindow;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.heba.Cfg;
import com.heba.bean.ItemAreaObject;
import com.heba.greenmulu.R;

import java.util.List;


/**
 * 地址弹出框
 * <p>
 * Created by shenghao on 2015/8/11.
 */
public class AddressPopupWindow extends BasePopupWindow {

    public ListView listView;
    public ItemListAdapter adapter;

    private OnAddressPopupClickListener onAddressPopupClickListener;

    public AddressPopupWindow(Context context) {
        super(LayoutInflater.from(context).inflate(
                R.layout.popup_address, null));
        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    public AddressPopupWindow(Context context, int width, int height) {
        super(context, LayoutInflater.from(context).inflate(
                R.layout.popup_address, null), width, height);
        setAnimationStyle(R.style.Popup_Animation_Translate);
    }

    @Override
    public void initViews() {
        listView = (ListView) findViewById(R.id.listview);
    }

    @Override
    public void initEvents() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onAddressPopupClickListener != null)
                    onAddressPopupClickListener.onItemClick(parent, view, position, id);
                dismiss();
            }
        });
    }

    @Override
    public void init() {

    }

    public void setOnCategoryPopupClickListener(OnAddressPopupClickListener onAddressPopupClickListener) {
        this.onAddressPopupClickListener = onAddressPopupClickListener;
    }

    /**
     * 回调接口 interface
     */
    public interface OnAddressPopupClickListener {

        void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    /**
     * ItemListAdapter
     */
    public static class ItemListAdapter extends BaseAdapter {

        private Context context;
        private List<ItemAreaObject> list;

        public ItemListAdapter(Context context, List<ItemAreaObject> list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(
                        R.layout.item_textview, null);
                viewHolder.textView = convertView.findViewById(R.id.textview);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            ItemAreaObject object = list.get(position);
            viewHolder.textView.setText(Cfg.Leng == 0 ? object.getLocationEN() : object.getLocationZH());
            return convertView;
        }

        class ViewHolder {
            TextView textView;
        }
    }

}
